package me.firstapp.service.weixin.account.impl;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.utils.DateUtils;
import me.firstapp.common.utils.MD5Utils;
import me.firstapp.module.weixin.jinbi.JinBiAccount;
import me.firstapp.module.weixin.jinbi.JinBiAccountTrace;
import me.firstapp.module.weixin.jinbi.JinBiAccountTrace.ChangeType;
import me.firstapp.module.weixin.system.AccountRecordType;
import me.firstapp.repository.weixin.jinbi.JinBiAccountRepository;
import me.firstapp.repository.weixin.jinbi.JinBiAccountTraceRepository;
import me.firstapp.service.weixin.account.JinBiAccountService;

@Service
public class JinBiAccountServiceImpl implements JinBiAccountService {

	@Autowired
	private JinBiAccountRepository jinBiAccountRepository;

	@Autowired
	private JinBiAccountTraceRepository jinBiAccountTraceRepository;

	@Override
	public JinBiAccount findJinBiAccountByCardNoAndOpenId(String cardNo, String openId) {
		return jinBiAccountRepository.findJinBiAccountByCardNoAndOpenId(cardNo, openId);
	}

	@Override
	public JinBiAccountTrace executeTzJinBiAccount(Long id, ChangeType changeType, BigDecimal tzValue) {
		JinBiAccount jinBiAccount = (JinBiAccount) jinBiAccountRepository.getById(id);
		JinBiAccountTrace jinBiAccountTrace = this.executeTzJinBiAccount(jinBiAccount, changeType, tzValue);
		return jinBiAccountTrace;
	}

	@Override
	public List<JinBiAccount> findJinBiAccountsForRank(int limitCount) {
		return jinBiAccountRepository.findJinBiAccountsForRank(limitCount);
	}

	@Override
	public JinBiAccount findById(Long id) {
		return (JinBiAccount) jinBiAccountRepository.getById(id);
	}

	@Override
	public JinBiAccountTrace executeTzJinBiAccount(JinBiAccount jinBiAccount, ChangeType changeType,
			BigDecimal tzValue) {
		if (!MD5Utils.verify(jinBiAccount.getBalance().doubleValue() * 10000 + "", jinBiAccount.getEncryption())) {
			throw new ServiceException("error", "账户异常，请联系客服！");
		}
		JinBiAccountTrace jinBiAccountTrace = new JinBiAccountTrace();
		jinBiAccountTrace.setJinBiAccountId(jinBiAccount.getId());
		jinBiAccountTrace.setChangeType(changeType);
		jinBiAccountTrace.setBeforeValue(jinBiAccount.getBalance());
		jinBiAccountTrace.setChangeValue(tzValue);
		BigDecimal afterValue = BigDecimal.ZERO;
		if (changeType.getAccountRecordType() == AccountRecordType.ADD) {
			afterValue = jinBiAccount.getBalance().add(tzValue).setScale(4, RoundingMode.HALF_UP);
		} else if (changeType.getAccountRecordType() == AccountRecordType.SUB) {
			afterValue = jinBiAccount.getBalance().subtract(tzValue).setScale(4, RoundingMode.HALF_UP);
		}
		jinBiAccountTrace.setAfterValue(afterValue);
		jinBiAccountTrace.setChangeDate(Integer.valueOf(DateUtils.getDateStr(new Date(), "yyyyMMdd")));
		jinBiAccountTrace.setChangeTime(new Date());
		jinBiAccountTraceRepository.save(jinBiAccountTrace);

		jinBiAccount.setBalance(afterValue);
		jinBiAccount.setEncryption(MD5Utils.generate(afterValue.doubleValue() * 10000 + ""));
		jinBiAccount.setUpdateTime(new Date());
		jinBiAccountRepository.update(jinBiAccount);
		return jinBiAccountTrace;
	}

}
